//
//  WebViewController.swift
//  SmartCheckIn
//
//  Created by Cidy Chou on 2017/10/24.
//  Copyright © 2017年 CidyChou. All rights reserved.
//

import Foundation
import UIKit
import WebKit

class WebViewController :  UIViewController ,WKNavigationDelegate,WKUIDelegate{
    
    let defaults = UserDefaults.standard
    var phone :String = "15521190200"
    
    var webview = WKWebView()
    
    @IBOutlet weak var back: UIButton!
    //var btnBack = UIBarButtonItem()
    var btnForward = UIBarButtonItem()
    
    var progBar = UIProgressView()
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        
        
        setNavBar()
        //获取导航栏高度
        //let navHeight = self.navigationController?.navigationBar.frame.height
        //获取状态栏高度
        //let statusHeight = UIApplication.shared.statusBarFrame.height
        
        //创建wkwebview
        //let webview = WKWebView(frame:CGRect(x: 0, y: statusHeight+navHeight!, width: self.view.bounds.size.width, height: view.bounds.height))
        
        //以下代码添加到viewDidLoad()
        
        
        webview = WKWebView(frame:CGRect(x: 0, y: 55, width: self.view.bounds.size.width, height: view.bounds.height))
        webview.navigationDelegate = self
        progBar = UIProgressView(frame: CGRect(x:0,y:0, width:self.view.frame.width,height: 30))
        progBar.progress = 0.0
        progBar.tintColor = UIColor.red
        webview.addSubview(progBar)
        
        
        webview.addObserver(self, forKeyPath: "estimatedProgress", options: NSKeyValueObservingOptions.new, context: nil)
        
        let array = defaults.array(forKey: "serverInfo")
        let ip = array![4]
        let api = array![3]
        let historyPath = "http://\(ip):\(api)/History/showHistory?phone=\(phone)"
        //["name": "广东水利电力职业学院", "api": "8085", "port": "9985", "ip": "210.21.85.178", "order": "5", "address": "dm.gdsdxy.cn"],
        
        print("historyPath\(historyPath)")
        //创建网址
        let url = NSURL(string: historyPath)
        //创建请求
        
        let request = URLRequest(url: url! as URL)
        //加载请求
        webview.load(request)
        //添加wkwebview
        self.view.addSubview(webview)
        
    }
    
    func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
        self.navigationItem.title = self.webview.title
    }
    
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        
        if keyPath == "estimatedProgress" {
            self.progBar.alpha = 1.0
            progBar.setProgress(Float(webview.estimatedProgress), animated: true)
            //进度条的值最大为1.0
            if(self.webview.estimatedProgress >= 1.0) {
                UIView.animate(withDuration: 0.3, delay: 0.1, options: UIViewAnimationOptions.curveEaseInOut, animations: { () -> Void in
                    self.progBar.alpha = 0.0
                }, completion: { (finished:Bool) -> Void in
                    self.progBar.progress = 0
                })
            }
        }
        
    }

    
    override func viewWillDisappear(_ animated: Bool) {
        webview.removeObserver(self, forKeyPath: "estimatedProgress")
    }
    
    
    func setNavBar() {
//        btnBack = UIBarButtonItem(title: "后退", style: UIBarButtonItemStyle.plain, target: self, action: #selector(WebViewController.toBack))
        btnForward = UIBarButtonItem(title: "前进", style: UIBarButtonItemStyle.plain, target: self, action: #selector(WebViewController.toForward))
        self.navigationItem.leftBarButtonItem = btnForward
        self.navigationItem.rightBarButtonItem = btnForward
    }
    
    @IBAction func back(_ sender: Any) {
     
        print("back")
        
        toBack()
    }
    
    func toBack() {
       // if self.webview.canGoBack {
            self.webview.goBack()
      //  }
    }
    
    func toForward() {
        if self.webview.canGoForward {
            self.webview.goForward()
        }
    }
    
    
}
